package suites.deeplink {
	import suites.AbstractConfig;
	import suites.stub.NavigationStub;

	import org.flexunit.asserts.assertEquals;
	import org.flexunit.asserts.assertTrue;
	import org.flexunit.asserts.fail;
	import org.splink.deepsplink.navigation.INavigation;

	/**
	 * @author Max Kugland
	 */
	public class NavigationTest extends AbstractDeeplinkControllerTest {

		[Test]
		public function startup() : void {
			var count : uint = 0;
			var navigationFnc : Function = function(id : String, params : Array, change : Boolean): void {
				assertEquals(0, params.length);				assertTrue(0, change);
				assertEquals(AbstractConfig.HOME, id);
				count++;
			};
			
			createCtrl();
			_ctrl.registerNavigation(new NavigationStub(navigationFnc));
			_ctrl.start();
			
			assertEquals(1, count);
			assertEquals(1, _ctrl.history.length);
		}
		
		[Test]
		public function twoStartup() : void {
			var count1 : uint = 0;
			var navigationFnc1 : Function = function(id : String, params : Array, change : Boolean): void {
				assertEquals(0, params.length);
				assertTrue(0, change);
				assertEquals(AbstractConfig.HOME, id);
				count1++;
			};
			var count2 : uint = 0;
			var navigationFnc2 : Function = function(id : String, params : Array, change : Boolean): void {
				assertEquals(0, params.length);
				assertTrue(0, change);
				assertEquals(AbstractConfig.HOME, id);
				count2++;
			};
			
			createCtrl();
			_ctrl.registerNavigation(new NavigationStub(navigationFnc1));			_ctrl.registerNavigation(new NavigationStub(navigationFnc2));
			_ctrl.start();
			
			assertEquals(1, count1);			assertEquals(1, count2);
			assertEquals(1, _ctrl.history.length);
		}
		
		[Test]
		public function goto() : void {
			var count : uint = 0;
			var navigationFnc : Function = function(id : String, params : Array, change : Boolean): void {
				assertEquals(0, params.length);
				assertTrue(0, change);
				assertEquals("2", id);
				count++;
			};
			
			createCtrl();
			_ctrl.registerNavigation(new NavigationStub(navigationFnc));
			_ctrl.gotoPage("2");
			_ctrl.start();
			
			assertEquals(1, count);
			assertEquals(1, _ctrl.history.length);
		}
		
		[Test]
		public function twoGoto() : void {
			var count1 : uint = 0;
			var navigationFnc1 : Function = function(id : String, params : Array, change : Boolean): void {
				assertEquals(0, params.length);
				assertTrue(0, change);
				assertEquals("2", id);
				count1++;
			};
			var count2 : uint = 0;
			var navigationFnc2 : Function = function(id : String, params : Array, change : Boolean): void {
				assertEquals(0, params.length);
				assertTrue(0, change);
				assertEquals("2", id);
				count2++;
			};
			
			createCtrl();
			_ctrl.registerNavigation(new NavigationStub(navigationFnc1));
			_ctrl.registerNavigation(new NavigationStub(navigationFnc2));
			_ctrl.gotoPage("2");
			_ctrl.start();
			
			assertEquals(1, count1);
			assertEquals(1, count2);
			assertEquals(1, _ctrl.history.length);
		}
		
		[Test]
		public function manyGotos() : void {
			var results : Array = ["1","2","3","4"];
			var count : uint = 0;
			var navigationFnc : Function = function(id : String, params : Array, change : Boolean): void {
				assertEquals(0, params.length);
				assertTrue(0, change);
				assertEquals(results[count], id);
				count++;
			};
			
			createCtrl();
			_ctrl.registerNavigation(new NavigationStub(navigationFnc));
			_ctrl.start();
			_ctrl.gotoPage("2");			_ctrl.gotoPage("3");			_ctrl.gotoPage("4");
			
			assertEquals(results.length, count);
			assertEquals(results.length, _ctrl.history.length);
		}
		
		[Test]
		public function remove() : void {
			var navigation : INavigation = new NavigationStub(function(id : String, params : Array, change : Boolean):void {
				fail("I should have been removed!");
			});
			
			createCtrl();
			_ctrl.registerNavigation(navigation);
			_ctrl.unregisterNavigation(navigation);
			_ctrl.start();
		}
		
	}
}
